#include "MainWindow.h"
#include <QtQuick/QQuickView>
#include "GraphicsView.h"
#include "SketchView.h"
#include "Message.h"
#include "MessageDispatcher.h"
#include "QuinceApplication.h"
#include "ToolBar.h"
#include "EditToolFactory.h"

MainWindow * MainWindow::self = NULL;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
  , mdiArea(new QMdiArea)
{
    self = this;
    setCentralWidget(mdiArea);

    windowMapper = new QSignalMapper(this);
    connect(windowMapper, SIGNAL(mapped(QWidget *)),
            this, SLOT(setActiveSubWindow(QWidget *)));
    createActions();
    createMenus();
    createToolBars();
    createStatusBar();
    readSettings();
    setWindowTitle(tr("Sketch"));
    setWindowIcon(QIcon(":/images/appicon.png"));
}

MainWindow::~MainWindow()
{
    self = NULL;
}

QWidget *
MainWindow::createMdiChild()
{
    QWidget *child = QWidget::createWindowContainer(new SketchView(), this);
    QMdiSubWindow *mdiWin = mdiArea->addSubWindow(child);
    mdiWin->setWindowIcon(QIcon(":/images/doc_sty.png"));
    mdiWin->showMaximized();
    static int windowNumber = 1;
    mdiWin->setWindowTitle(tr("Untitled %1").arg(windowNumber++));

    return child;
}

void
MainWindow::setActiveSubWindow(QWidget *window)
{
    if(window == nullptr) return;

    mdiArea->setActiveSubWindow(qobject_cast<QMdiSubWindow *>(window));
}

void
MainWindow::createActions()
{
    newAct = new QAction(QIcon(":/images/new.png"), tr("&New..."), this);
    newAct->setShortcut(tr("Ctrl+N"));
    newAct->setStatusTip(tr("Create a new file"));
    connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));

    openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
    openAct->setShortcut(tr("Ctrl+O"));
    openAct->setStatusTip(tr("Open an existing file"));
    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

    saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
    saveAct->setShortcut(tr("Ctrl+S"));
    saveAct->setStatusTip(tr("Save the document to disk"));
    connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));

    saveAsAct = new QAction(tr("Save &As..."), this);
    saveAsAct->setStatusTip(tr("Save the document under a new name"));
    connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));


    exitAct = new QAction(tr("E&xit"), this);
    exitAct->setShortcut(tr("Ctrl+Q"));
    exitAct->setStatusTip(tr("Exit the application"));
    connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));

    cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
    cutAct->setShortcut(tr("Ctrl+X"));
    cutAct->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
    connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));

    copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
    copyAct->setShortcut(tr("Ctrl+C"));
    copyAct->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
    connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));

    pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
    pasteAct->setShortcut(tr("Ctrl+V"));
    pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
    connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));

    closeAct = new QAction(tr("Cl&ose"), this);
    closeAct->setShortcut(tr("Ctrl+F4"));
    closeAct->setStatusTip(tr("Close the active window"));
    connect(closeAct, SIGNAL(triggered()), mdiArea, SLOT(closeActiveSubWindow()));

    closeAllAct = new QAction(tr("Close &All"), this);
    closeAllAct->setStatusTip(tr("Close all the windows"));
    connect(closeAllAct, SIGNAL(triggered()), mdiArea, SLOT(closeAllSubWindows()));

    tileAct = new QAction(tr("&Tile"), this);
    tileAct->setStatusTip(tr("Tile the windows"));
    connect(tileAct, SIGNAL(triggered()), mdiArea, SLOT(tileSubWindows()));

    cascadeAct = new QAction(tr("&Cascade"), this);
    cascadeAct->setStatusTip(tr("Cascade the windows"));
    connect(cascadeAct, SIGNAL(triggered()), mdiArea, SLOT(cascadeSubWindows()));

    nextAct = new QAction(tr("Ne&xt"), this);
    nextAct->setStatusTip(tr("Move the focus to the next window"));
    connect(nextAct, SIGNAL(triggered()), mdiArea, SLOT(activateNextSubWindow()));

    previousAct = new QAction(tr("Pre&vious"), this);
    previousAct->setStatusTip(tr("Move the focus to the previous window"));
    connect(previousAct, SIGNAL(triggered()), mdiArea, SLOT(activatePreviousSubWindow()));

    separatorAct = new QAction(this);
    separatorAct->setSeparator(true);

    aboutAct = new QAction(tr("&About Pattern"), this);
    aboutAct->setStatusTip(tr("About PAD System Pattern"));
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
}

void
MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(newAct);
    fileMenu->addAction(openAct);
    fileMenu->addAction(saveAct);
    fileMenu->addAction(saveAsAct);

    fileMenu->addSeparator();

    fileMenu->addAction(exitAct);

    editMenu = menuBar()->addMenu(tr("&Edit"));
    editMenu->addAction(cutAct);
    editMenu->addAction(copyAct);
    editMenu->addAction(pasteAct);

    windowMenu = menuBar()->addMenu(tr("&Window"));
    updateWindowMenu();
//    connect(windowMenu, SIGNAL(aboutToShow()), this, SLOT(updateWindowMenu()));


    menuBar()->addSeparator();

    helpMenu = menuBar()->addMenu(tr("&Help"));
    helpMenu->addAction(aboutAct);
}

void
MainWindow::updateWindowMenu()
{
    windowMenu->clear();
    windowMenu->addAction(closeAct);
    windowMenu->addAction(closeAllAct);
    windowMenu->addSeparator();
    windowMenu->addAction(tileAct);
    windowMenu->addAction(cascadeAct);
    windowMenu->addSeparator();
    windowMenu->addAction(nextAct);
    windowMenu->addAction(previousAct);
    windowMenu->addAction(separatorAct);
}

void
MainWindow::createToolBars()
{
    fileToolBar = addToolBar(tr("File"));
    fileToolBar->addAction(newAct);
    fileToolBar->addAction(openAct);
    fileToolBar->addAction(saveAct);

    editToolBar = addToolBar(tr("Edit"));
    editToolBar->addAction(cutAct);
    editToolBar->addAction(copyAct);
    editToolBar->addAction(pasteAct);

    ToolBar *toolBar = new ToolBar(tr("ToolBar"), this, 2);
    addToolBar(Qt::LeftToolBarArea, toolBar);

    toolBar->loadTools(new EditToolFactory());
}

void
MainWindow::createStatusBar()
{
    statusBar()->showMessage(tr("Ready"));
}

void
MainWindow::readSettings()
{
    QSettings settings;
    QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
    QSize size = settings.value("size", QSize(400, 400)).toSize();
    move(pos);
    resize(size);
}

void
MainWindow::writeSettings()
{
    QSettings settings;
    settings.setValue("pos", pos());

    QPointF posa;
    posa = pos();
    settings.setValue("size", size());
}

void
MainWindow::newFile()
{
    Message msg("newdoc");
    QuinceApp().localDispatcher().dispatch(msg);
}

void
MainWindow::open()
{

}

void
MainWindow::save()
{
//    shared_ptr<SketchDocument> doc = QuinceApp().
//	if (doc != NULL) {
//		QString fileName = QFileDialog::getSaveFileName(this,tr("Save Pattern"),"",tr("Style File (*.sty);;Base File (*.bas);;PAD File (*.pad);;All File (*.*)"));
//		if (!fileName.isEmpty()) {
//			Message msg("savedoc");
//			msg.addContent("file", fileName);
//			msg.addContent("docid", doc->id().value());
//			Application().localDispatcher().dispatch(msg);
//		}
//	}
}

void
MainWindow::saveAs()
{

}

void
MainWindow::cut()
{
    //activeMdiChild()->cut();
}

void
MainWindow::copy()
{
    //activeMdiChild()->copy();
}

void
MainWindow::paste()
{
    //activeMdiChild()->paste();
}

void MainWindow::about()
{
    QMessageBox::about(this, tr("About Quince Sketch"),
            tr("<b>Quince &reg; v1.0.0.0</b> Sketch Module<br /><br />&copy; 2013- Neuron Co. Limited"));
}

QWidget *
MainWindow::activeMdiChild()
{
    QMdiSubWindow *activeSubWindow = mdiArea->activeSubWindow();
    if(activeSubWindow != NULL)
    {
        return qobject_cast<QWidget *>(activeSubWindow->widget());
    }
    return NULL;
}

QMdiSubWindow *
MainWindow::findMdiChild(const QString &fileName)
{
    QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();

    //foreach (QMdiSubWindow *window, mdiArea->subWindowList()) {
        //QWidget *mdiChild = window->widget();
       // if (mdiChild != NULL && mdiChild->currentFile() == canonicalFilePath)
            //return window;
    //}
    return 0;
}

void
MainWindow::switchLayoutDirection()
{
    qApp->setLayoutDirection(layoutDirection() == Qt::LeftToRight ? Qt::RightToLeft : Qt::LeftToRight);
}

